#菜品信息视图文件
from django.shortcuts import render
from django.http import HttpResponse
from django.http import JsonResponse
from django.core.paginator import Paginator
from datetime import datetime
from myadmin.models import Category, Shop
from myadmin.models import Product
import time,os


# Create your views here.
def index(request,pIndex=1):
	umod = Product.objects
	ulist = umod.filter(status__lt=29)
	mywhere=[]
	#获取并且判断搜索条件
	kw = request.GET.get("keyword",None)
	if kw:
		ulist = ulist.filter(name__contains=kw)
		mywhere.append('keyword='+kw)
	cid = request.GET.get("category_id",None)
	if cid:
		ulist = ulist.filter(category_id=cid)
		mywhere.append('keyword='+cid)
	#添加状态的搜索条件
	ulist = umod.filter(status__lt=20)
	mywhere=[]
	#获取并且判断搜索条件
	status = request.GET.get("status",'')
	if status != '':
		ulist = ulist.filter(status=status)
		mywhere.append('status='+status)
		
	ulist = ulist.order_by('id')
	#执行分页操作
	pIndex = int(pIndex)
	page = Paginator(ulist,5)#以每页五条数据分页
	maxpages = page.num_pages#获取最大页数
	#判定当前页是否越界
	if pIndex > maxpages:
		pIndex = maxpages
	if pIndex < 1:
		pIndex = 1
	list2 = page.page(pIndex)#获取当前页数据
	plist = page.page_range#获取页码列表信息

	for vo in list2:#这里是一个跨表查询
		sob = Shop.objects.get(id=vo.shop_id)
		vo.shopname = sob.name
		cob = Category.objects.get(id=vo.category_id)
		vo.categoryname = cob.name

	context = {"productlist":list2,'plist':plist,'pIndex':pIndex,'maxpages':maxpages,'mywhere':mywhere}
	return render(request,"myadmin/product/index.html",context)
#浏览信息
def loadCategory(request,sid):
	clist = Category.objects.filter(status__lt=9,shop_id=sid).values("id","name")
	return JsonResponse({'data':list(clist)})

def add(request):
	slist = Shop.objects.values("id",'name')
	context = {"shoplist":slist}
	return render(request,'myadmin/product/add.html',context)#加载信息

def insert(request):
	try:
		myfile = request.FILES.get("cover_pic",None)
		if not myfile:
			return HttpResponse("没有店铺封面上传文件信息")
		cover_pic = str(time.time())+"."+myfile.name.split('.').pop()
		destination = open("./static/uploads/product/"+cover_pic,"wb+")
		for chunk in myfile.chunks():
			destination.write(chunk)
		destination.close()

		ob = Product()
		ob.shop_id = request.POST['shop_id']
		ob.category_id = request.POST['category_id']
		ob.name = request.POST['name']
		ob.price = request.POST['price']
		ob.cover_pic = cover_pic
		ob.status = 1
		ob.create_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
		ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
		ob.save()
		context = {'info':"添加成功"}
	except Exception as err:
		print(err)
		context = {'info':"添加失败"}
	return render(request,"myadmin/info.html",context)

#执行信息

def delete(request,pid=0):
	try:
		ob = Product.objects.get(id=pid)
		ob.status = 9
		ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
		ob.save()
		context = {'info':"删除成功！"}
	except Exception as err:
		print(err)
		context = {'info':"删除失败"}
	return render(request,"myadmin/info.html",context)

def edit(request,pid=0):
	try:
		ob = Product.objects.get(id=pid)
		context = {'product':ob }
		slist = Shop.objects.values("id",'name')
		context ["shoplist"] = slist
		ob.save()
		return render(request,"myadmin/product/edit.html",context)
	except Exception as err:
		print(err)
		context = {'info':"没有找到要修改的数据信息"}
		return render(request,"myadmin/info.html",context)


def update(request,pid):
	try:#获取原图片
		oldpicname = request.POST["oldpicname"]
		myfile = request.FILES.get("cover_pic",None)
		if not myfile:
			cover_pic = oldpicname
		else:
			cover_pic = str(time.time())+"."+myfile.name.split('.').pop()
			destination = open("./static/uploads/product/"+cover_pic,"wb+")
			for chunk in myfile.chunks():
				destination.write(chunk)
		destination.close()

		ob = Product.objects.get(id=pid)
		ob.shop_id = request.POST['shop_id']
		ob.category_id = request.POST['category_id']
		ob.price = request.POST['price']
		ob.name = request.POST['name']
		ob.cover_pic = cover_pic
		ob.update_at = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
		ob.save()
		context = {'info':"修改成功"}
		if myfile:
			os.remove("./static/uploads/product/"+oldpicname)

	except Exception as err:
		print(err)
		context = {'info':"修改失败"}
		if myfile:
			os.remove("./static/uploads/product/"+cover_pic)
	return render(request,"myadmin/info.html",context)